﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Scheduler.Presentation_Layer
{
    /**Class: PasswordReset
     * @author: Hieu Nhan (Kevin)
     * @version: 1.1
     * Course: ITEC 4860 Spring, 2012
     * Written: March XX, 2012
     * This class - Presents a panel that allows managers to reset the passwords of employees
     * Purpose: Allows for passwords to be set/reset
     */

    public partial class PasswordReset : Form
    {
        // Data Access Layer for the Account Management
        private Data_Access_Layer.AcctManagement accountManagement;
        // Data Access Layer for the Login 
        private Data_Access_Layer.Login login;

        /**Method: PasswordReset constructor
         * Initializes the two ArrayLists and also creates and dictates
         * how to populate the employeeListComboBox
         */

        public PasswordReset()
        {
            InitializeComponent();
        }


        /**Method: resetButton_Click
         * calls the passwordReset method that takes the userID selected from
         * the employeeListComboBox, and checks to see if that employee exist.
         * If so, allows the manager to change their password and also set their
         * AccountLocked status to false.
         */

        private void resetButton_Click_1(object sender, EventArgs e)
        {
            passwordReset();
        }

        /**Method: keyPressed
         * calls the passwordReset method that takes the userID selected from
         * the employeeListComboBox, and checks to see if that employee exist.
         * If so, allows the manager to change their password and also set their
         * AccountLocked status to false when the enter key is pressed while
         * in the password textbox
         */

        private void keyPressed(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                passwordReset();
            }
        }

        /**Method: passwordReset
         * Takes the userID used to login and checks to see if that account is a manager.
         * Enter the current password, the new password, and then confirm the new password by 
         * retyping it. If the new password and the confirm one are the same, then password is reset 
         * to what the new password is.
         * Displays an error messagebox if the new password and the confirm one does not match.
         */

        private void passwordReset()
        {
            accountManagement = new Data_Access_Layer.AcctManagement(Scheduler.Presentation_Layer.GlobalVariables.GlobalLoggedInUser.Trim());
            
            login = new Data_Access_Layer.Login(Scheduler.Presentation_Layer.GlobalVariables.GlobalLoggedInUser, currentPasswordTextBox.Text);

            if (accountManagement.locateUser() == true && login.isManager() == true && newPasswordTextBox.Text == confirmPasswordTextBox.Text)
            {
                accountManagement.setPassword(newPasswordTextBox.Text);
                accountManagement.setIsAcctLocked(false);

                String successMessage = "Password for " + accountManagement.getName() + " has been reset." + "\n"
                + "Password has been set to: " + newPasswordTextBox.Text + "\n"
                + "The accountlock status is set to: " + accountManagement.getIsAcctLocked();

                MessageBox.Show(successMessage, "Password Reset", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

            if (newPasswordTextBox.Text != confirmPasswordTextBox.Text)
            {
                MessageBox.Show("New Password does not match!", "Password Reset Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        /**Method: cancelButton_Click
         * closes this form
         */

        private void cancelButton_Click_1(object sender, EventArgs e)
        {
            this.Close();
        }

        private void PasswordReset_Load(object sender, EventArgs e)
        {

        }
    }
}
